package com.localcache.server.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.system.SystemPropsKeys;
import cn.hutool.system.SystemUtil;
import com.distribute.common.MsgObj;
import com.distribute.common.MsgType;
import com.localcache.server.cache.CacheConstants;
import com.localcache.server.context.ChannelContextManager;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * TODO
 *
 * @author chennengcheng
 * @date 2022-05-25 15:22
 * @remark
 */

@Service
public class DlcCacheServerService {

    private static final Logger log = LoggerFactory.getLogger(DlcCacheServerService.class);

    public static final Map<String, Boolean> fileDownloadStatus = new ConcurrentHashMap<>();

    public void flushCurrentCacheData(String key) {
        ChannelHandlerContext channelHandlerContext = ChannelContextManager.channelHandlerContextMap.get(key);
        if (Objects.isNull(channelHandlerContext)) {
            log.error("找不到对应的缓存：" + key);
            return;
        }

        Channel channel = channelHandlerContext.channel();
        if (channel.isActive()) {
            String[] split = key.split(StrUtil.COLON );
            String modelPrefix = split[3];
            String model = split[4];
            String modelName = modelPrefix + StrUtil.COLON + model;
            MsgObj msgObj = new MsgObj(MsgType.FLUSH_CACHE_INFO_DATA, key, modelName);
            log.info("正在刷新 内存数据 " + msgObj);
            channel.writeAndFlush(JSONUtil.toJsonStr(msgObj));
        }
    }

    public void openCache(String key) {
        ChannelHandlerContext channelHandlerContext = ChannelContextManager.channelHandlerContextMap.get(key);
        Channel channel = channelHandlerContext.channel();
        if (channel.isActive()) {
            String[] split = key.split(StrUtil.COLON );
            String modelPrefix = split[3];
            String model = split[4];
            String modelName = modelPrefix + StrUtil.COLON + model;
            MsgObj msgObj = new MsgObj(MsgType.OPEN_CACHE_DATA, key, modelName);
            log.info("正在开启缓存 " + key);
            channel.writeAndFlush(JSONUtil.toJsonStr(msgObj));
        }
    }

    public void closeCache(String key) {
        ChannelHandlerContext channelHandlerContext = ChannelContextManager.channelHandlerContextMap.get(key);
        Channel channel = channelHandlerContext.channel();
        if (channel.isActive()) {
            String[] split = key.split(StrUtil.COLON );
            String modelPrefix = split[3];
            String model = split[4];
            String modelName = modelPrefix + StrUtil.COLON + model;
            MsgObj msgObj = new MsgObj(MsgType.CLOSE_CACHE_DATA, key, modelName);
            log.info("正在关闭缓存 " + key);
            channel.writeAndFlush(JSONUtil.toJsonStr(msgObj));
        }
    }

    public void getCurrentCache(String key){
        ChannelHandlerContext channelHandlerContext = ChannelContextManager.channelHandlerContextMap.get(key);
        if (Objects.isNull(channelHandlerContext)) {
            return;
        }
//
//        Boolean isDownloading = DlcCacheServerService.fileDownloadStatus.get(key);
//        if (Objects.nonNull(isDownloading) && isDownloading) {
//            log.error("正在下载实时缓存数据中: " + key);
//            return;
//        }
//
//        Boolean isGenerator = CacheConstants.tempCacheFileGenerateStatusMap.get(key);
//        if (Objects.nonNull(isGenerator) && isGenerator) {
//            log.error("正在生成实时缓存数据中: " + key);
//            return;
//        }

        CacheConstants.tempCacheFileGenerateStatusMap.put(key, true);

        Channel channel = channelHandlerContext.channel();
        if (channel.isActive()) {
            String[] split = key.split(StrUtil.COLON );
            String modelPrefix = split[3];
            String model = split[4];
            String modelName = modelPrefix + StrUtil.COLON  + model;
            MsgObj msgObj = new MsgObj(MsgType.GET_CACHE_INFO_DATA, key, modelName);
            channel.writeAndFlush(JSONUtil.toJsonStr(msgObj));
        }
    }
}
